package com.bootdo.fx.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.fx.domain.GoodsUserCommentDO;
import com.bootdo.fx.service.GoodsUserCommentService;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * 
 * @author 袁飞龙
 * @email 1992lcg@163.com
 * @date 2017-11-30 17:12:20
 */
 
@Controller
public class GoodsUserCommentController {
	@Autowired
	private GoodsUserCommentService goodsUserCommentService;

	@Autowired
	private IdWorker idWorker;
	
	@GetMapping("/fx/goodsUserComment")
	@RequiresPermissions("fx:goodsUserComment:goodsUserComment")
	String GoodsUserComment(){
	    return "fx/goodsUserComment/goodsUserComment";
	}
	
	@ResponseBody
	@GetMapping("/fx/goodsUserComment/list")
	@RequiresPermissions("fx:goodsUserComment:goodsUserComment")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<GoodsUserCommentDO> goodsUserCommentList = goodsUserCommentService.list(query);
		int total = goodsUserCommentService.count(query);
		PageUtils pageUtils = new PageUtils(goodsUserCommentList, total);
		return pageUtils;
	}
	
	@GetMapping("/fx/goodsUserComment/add")
	@RequiresPermissions("fx:goodsUserComment:add")
	String add(){
	    return "fx/goodsUserComment/add";
	}

	@GetMapping("/fx/goodsUserComment/edit/{commenId}")
	@RequiresPermissions("fx:goodsUserComment:edit")
	String edit(@PathVariable("commenId") Long commenId,Model model){
		GoodsUserCommentDO goodsUserComment = goodsUserCommentService.get(commenId);
		model.addAttribute("goodsUserComment", goodsUserComment);
	    return "fx/goodsUserComment/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/fx/goodsUserComment/save")
	@RequiresPermissions("fx:goodsUserComment:add")
	public R save( GoodsUserCommentDO goodsUserComment){
		if(goodsUserCommentService.save(goodsUserComment)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/fx/goodsUserComment/update")
	@RequiresPermissions("fx:goodsUserComment:edit")
	public R update( GoodsUserCommentDO goodsUserComment){
		goodsUserCommentService.update(goodsUserComment);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/goodsUserComment/remove")
	@ResponseBody
	@RequiresPermissions("fx:goodsUserComment:remove")
	public R remove( Long commenId){
		if(goodsUserCommentService.remove(commenId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/goodsUserComment/batchRemove")
	@ResponseBody
	@RequiresPermissions("fx:goodsUserComment:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] commenIds){
		goodsUserCommentService.batchRemove(commenIds);
		return R.ok();
	}


	/*
  	* 根据商品ID获取该商品的评论
 	 * */
	@RequestMapping("/wx/goodsUserComment/getGoodsComment")
	@ResponseBody
	public JsonResult getGoodsComment(String goodsId,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> map = new HashMap<>();
		map.put("goodsId",goodsId);

		return ResultUtil.renderSuccess(goodsUserCommentService.list(map));
	}

	/*
    * 发布评论
    * */
	@RequestMapping("/wx/goodsUserComment/GoodsUserComment")
	@ResponseBody
	public JsonResult GoodsUserComment(GoodsUserCommentDO goodsUserComment, HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		goodsUserComment.setCommenId(idWorker.nextId());
		goodsUserComment.setCommentTime(sdf.format(new Date()));
		int b = goodsUserCommentService.save(goodsUserComment);
		if(b > 0){
			return ResultUtil.renderSuccess("发布成功！");
		}else{
			return ResultUtil.renderError("发布失败！");
		}
	}

}
